package com.ghn.cocknovel.ui.activity.main

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.PopupWindow
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.example.basemodel.base.BaseHeadVMActivity
import com.example.basemodel.base.DefaultHeadStyle
import com.ghn.cocknovel.R
import com.ghn.cocknovel.databinding.ActivityPictureBinding
import com.ghn.cocknovel.ui.activity.main.vm.MainViewModel
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions

class PictureActivity :
    BaseHeadVMActivity<MainViewModel, ActivityPictureBinding, DefaultHeadStyle>() {
    private var pop: PopupWindow? = null
    override fun layoutId() = R.layout.activity_picture
    override fun initView(savedInstanceState: Bundle?) {
        mHeadStyle.setTitleColor("头像", getColor(R.color.black))
        //抽屉布局中的司机信息
        mViewModel.getmessage()
        mViewModel.message.observe(this) {
            Glide
                .with(this)
                .load(it?.driverAvatar)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .into(mDatabind.pictureImage)
        }
        mDatabind.pictures.setOnClickListener {
            XXPermissions.with(this)
                .permission(Permission.WRITE_EXTERNAL_STORAGE)
                .permission(Permission.CAMERA)
                .request(object : OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (!allGranted) {

                            return
                        } else {
                            showpop()
                        }
                    }

                })
        }

    }

    private fun checkPicturePermission() {


    }

    private fun showpop() {
        val bottomView =
            View.inflate(this, R.layout.layout_bottom_dialog, null)
        val mAlbum = bottomView.findViewById<TextView>(R.id.tv_album)
        val mCamera = bottomView.findViewById<TextView>(R.id.tv_camera)
        val mCancel = bottomView.findViewById<TextView>(R.id.tv_cancel)

        pop = PopupWindow(bottomView, -1, -2)
        pop?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        pop?.setOutsideTouchable(true)
        pop?.setFocusable(true)
        val lp = window.attributes
        lp.alpha = 0.5f
        window.attributes = lp
        pop?.setOnDismissListener(PopupWindow.OnDismissListener {
            val lp = window.attributes
            lp.alpha = 1f
            window.attributes = lp
        })
        pop?.setAnimationStyle(R.style.main_menu_photo_anim)
        pop?.showAtLocation(window.decorView, Gravity.BOTTOM, 0, 0)

        val clickListener =
            View.OnClickListener { view ->
                when (view.id) {
                    R.id.tv_album ->                         //相册
                        checkPicturePermission()

                    R.id.tv_camera ->                         //拍照
                        checkCameraPermission()

                    R.id.tv_cancel -> {}
                }
                closePopupWindow()
            }

        mAlbum.setOnClickListener(clickListener)
        mCamera.setOnClickListener(clickListener)
        mCancel.setOnClickListener(clickListener)
    }

    private fun checkCameraPermission() {

    }

    fun closePopupWindow() {
        if (pop != null && pop!!.isShowing) {
            pop!!.dismiss()
            pop = null
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

    }

    override fun createObserver() {

    }
}